package com.example.apple.ddhr_new.activity.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.apple.ddhr_new.R;
import com.example.lzbase.BaseViewModel;
import com.example.lzbase.BaseViewModelActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;


import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.ClassUtil;

public abstract class BaseListViewActivity<T extends BaseViewModel> extends BaseViewModelActivity {


    @BindView(R.id.recycle_view)
    protected RecyclerView recycleView;
    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_data)
    protected TextView tvData;
    @BindView(R.id.l_empty)
    protected LinearLayout lEmpty;
    protected T listViewModel;
    protected ArrayList dataArray;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_list_view;

    }

    @Override
    protected void created(@Nullable Bundle savedInstanceState) {
        super.created(savedInstanceState);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                headerFreshing();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                footerFreshing();
            }
        });

        recycleView.setLayoutManager(new LinearLayoutManager(this));

    }

    protected void headerFreshing() {
        headerFreshEnd();

    }

    protected void footerFreshing() {
        footerFreshEnd();
    }

    protected void headerFresh() {
        refreshLayout.autoRefresh();

    }

    protected void footerFresh() {
        refreshLayout.autoLoadMore();
    }

    protected void headerFreshEnd() {
        refreshLayout.finishRefresh();

    }

    protected void footerFreshEnd() {
        refreshLayout.finishLoadMore();
    }

    protected void showEmpty(boolean isEmpty) {
        if(isEmpty){

//            refreshLayout.setVisibility(View.GONE);
            lEmpty.setVisibility(View.VISIBLE);
        }else
        {
//            refreshLayout.setVisibility(View.VISIBLE);
            lEmpty.setVisibility(View.GONE);
        }
    }



    @OnClick(R.id.btn_reload)
    public void onViewClicked() {

        reload();

    }
    protected  void  reload(){

    }

    @Override
    protected void initViewModel() {
        Class<T> viewModelClass = ClassUtil.getViewModel(this);
        if (viewModelClass != null) {
            ViewModelProvider viewModelProvider = new  ViewModelProvider(this.getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication()));

            this.listViewModel= viewModelProvider.get(viewModelClass);
        };


        if(this.listViewModel!=null){
            this.listViewModel.error.observe(this,error->{

                onError(error);
            });
            this.listViewModel.progress.observe(this,show->{
                showLoading(show);
            });
        }
    }
}
